﻿using OpenAuth.Repository;
using System;
using System.Collections.Generic;
using System.Text;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using System.Threading.Tasks;
using Infrastructure;
using OpenAuth.Repository.Domain;
using OpenAuth.App.Interface;
using OpenAuth.Repository.Interface;
using System.Linq;

namespace OpenAuth.App
{
   public class MST_MATECODEApp :BaseApp<MST_MATECODE, OpenAuthDBContext>
    {

        /// <summary>
        /// 根据TypeId加载
        /// </summary>
        public async Task<TableData> Load(MST_MATECODEListReq request)
        {
         
            var loginContext = _auth.GetCurrentUser();
            if (loginContext == null)
            {
                throw new CommonException("登录已过期", Define.INVALID_TOKEN);
            }
            /*
             var result = new TableData();
            var forms = GetDataPrivilege("u");
            if (!string.IsNullOrEmpty(request.key))
            {
                forms = forms.Where(u => u.Name.Contains(request.key) || u.Id.Contains(request.key));
            }
            result.data = forms.OrderByDescending(u => u.CreateDate)
                .Skip((request.page - 1) * request.limit)
                .Take(request.limit).ToList();
            result.count = forms.Count();
            return result;
             
             */
            var result = new TableData();
            var objs = UnitWork.Find<MST_MATECODE>(null);
            

            if (!string.IsNullOrEmpty(request.TypeId))
            {
                objs = objs.Where(u => u.TYPE_ID == request.TypeId);
            }
            if (!string.IsNullOrEmpty(request.key)) {
                objs = objs.Where(u => u.Mate_Code.Contains(request.key) || u.Mate_Descr.Contains(request.key));
            }
            result.data = objs.OrderBy(u => u.Mate_Code).Skip((request.page - 1) * request.limit).Take(request.limit).ToList();
            result.count = objs.Count();
            return result; 
        }
     
        /// <summary>
        /// 加载所有节点物项
        /// </summary>
        public async Task<TableData> LoadNode(MST_MATECODEListReq request)
        {

            var loginContext = _auth.GetCurrentUser();
            if (loginContext == null)
            {
                throw new CommonException("登录已过期", Define.INVALID_TOKEN);
            }
            /*
             var result = new TableData();
            var forms = GetDataPrivilege("u");
            if (!string.IsNullOrEmpty(request.key))
            {
                forms = forms.Where(u => u.Name.Contains(request.key) || u.Id.Contains(request.key));
            }
            result.data = forms.OrderByDescending(u => u.CreateDate)
                .Skip((request.page - 1) * request.limit)
                .Take(request.limit).ToList();
            result.count = forms.Count();
            return result;
             
             */
            var result = new TableData();
            var objs = UnitWork.Find<MST_MATECODE>(null);


            if (!string.IsNullOrEmpty(request.IsNode))
            {
                objs = objs.Where(u => u.IsNode == request.IsNode);
            }
            if (!string.IsNullOrEmpty(request.key))
            {
                objs = objs.Where(u => u.Mate_Code.Contains(request.key) || u.Mate_Descr.Contains(request.key));
            }
            result.data = objs.OrderBy(u => u.Mate_Code).ToList();
            result.count = objs.Count();
            return result;
        }
        public MST_MATECODEApp(IUnitWork<OpenAuthDBContext> unitWork, IRepository<MST_MATECODE, OpenAuthDBContext> repository, IAuth auth) : base(unitWork, repository, auth)
        {
        }
    }
}
